package com.hzjh.h5mall.ui

import android.view.KeyEvent
import android.widget.FrameLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.frame.JHBaseVM
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.style.weight.ToastKt.toast
import com.hzjh.h5mall.databinding.ActivityMainBinding
import com.hzjh.h5mall.ui.web.JHBaseWebActivity
import kotlin.system.exitProcess

@Route(path = RouteUrl.HOME_ACTIVITY_MAIN)
class MainActivity : JHBaseWebActivity<ActivityMainBinding, JHBaseVM>() {

    override fun getWebFlContent(): FrameLayout {
        return viewBinding.flContent
    }

    override fun initViewBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    /**
     * 两次返回退出程序
     */
    private var firstTime: Long = 0
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        val secondTime = System.currentTimeMillis()
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(mWebView.canGoBack()){
                mWebView.goBack()
            }else if (secondTime - firstTime < 2000) {
                exitProcess(0)
            } else {
                toast("Press again to exit the program")
                firstTime = System.currentTimeMillis()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}